package cn.easy.rpc.core;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 注册表
 * @author zwb
 * @create 2020-09-06 17:33
 **/
public class RegistrationTable implements Serializable {

    private static final Log logger = LogFactory.getLog(RegistrationTable.class);
    /**
     * serviceId TO RegisterTable
     */
    private static final Map<String, ServiceRegistrar> registerTableMap = new ConcurrentHashMap<>();

    public static void register(ServiceRegistrar registrar) {
        registerTableMap.put(registrar.getServiceId(), registrar);
    }

    public static void offline(String serviceId) {
        logger.warn("A service node ware offline,serviceId："+serviceId);
        registerTableMap.remove(serviceId);
    }

    public static CopyOnWriteArrayList<ServiceRegistrar> getRegisterTable() {
       return new CopyOnWriteArrayList<>(registerTableMap.values());
    }
}
